<template>
<div>
  <el-dialog :visible.sync="isvisible">
    <thingedit :thingForm="thingForm" @saveThing="saveThing"></thingedit>
  </el-dialog>
  <el-row>
     <el-col :span="24" class="demo-handle">
     <el-button type='info' icon='plus'
                @click='handleNew'>新建事件</el-button>
           <el-form :model='queryform' :inline="true" class="demo-form-inline">
                <el-form-item>
                    <el-input v-model="queryform.thTitle" placeholder="事件标题"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="info" icon='search' @click='queryThing'>查询</el-button>
                </el-form-item>
            </el-form>
        </el-col>
  </el-row>
  <el-row>
    <el-table
      highlight-current-row
      stripe
      border
      :row-key="getRowKeys"
      :expand-row-keys="expands"
      :data="proData">
      <el-table-column
        type="index"
        width="60">
      </el-table-column>
      <el-table-column type="expand">
        <template scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="客户端IP">
                <span>{{ props.row.thIP }}</span>
              </el-form-item>
            <el-form-item label="创建时间">
              <span>{{ props.row.thCreateDate }}</span>
            </el-form-item>
            <el-form-item label="最近更新人">
              <span>{{ props.row.thLastUpdator }}</span>
            </el-form-item>
            <el-form-item label="最近更新时间">
              <span>{{ props.row.thLastUpdateTime }}</span>
            </el-form-item>
            <el-form-item label="事件内容">
              <span>{{ props.row.thContent }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column
        label="事件标题"
        prop="thTitle" width="400">
      </el-table-column>
      <el-table-column
        label="事件内容"
        prop="thContent">
      </el-table-column>
      <el-table-column
        label="创建人"
        prop="thCreator" width="100">
      </el-table-column>
      <el-table-column
        label="工作量"
        prop="thWorkNum" width="100">
        <template scope="props">
          <span>{{props.row.thWorkNum}}小时</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template scope="scope">
          <el-button
            size="small"
            type="success"
            icon="edit"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="small"
            type="danger"
            icon="delete"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-row>
</div>
</template>

<style>
  .demo-handle{
    padding-top: 20px;
    padding-left: 20px;
  }
  .demo-form-inline{
    display: inline-block;
    float: right;
  }
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 120px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>

<script>
  import thingedit from './thing-edit'
  import axios from 'axios'
  import moment from 'moment'
  export default {
    data () {
      return {
         // 获取row的key值
        getRowKeys (row) {
          return row.id
        },
        thingForm: {},
        queryform: {
          thTitle: '',
          proId: this.$store.state.proId
        },
        isvisible: false,
        ops: '',
        // 要展开的行，数值的元素是row的key值
        expands: [],
        proData: []
      }
    },
    components: {
      thingedit
    },
    methods: {
      handleNew () {
        this.thingForm = {
          index: 0,
          id: '',
          thTitle: '',
          thCreator: '',
          thWorkNum: 0,
          thCreateDate: '',
          thLastUpdateTime: '',
          thLastUpdator: '',
          thContent: ''
        }
        this.opts = 'create'
        this.isvisible = true
      },
      handleThings (index, row) {
        this.$router.replace('/thing')
      },
      handleEdit (index, row) {
        this.thingForm = Object.assign({
          index: 0,
          id: '',
          thTitle: '',
          thCreator: '',
          thWorkNum: 0,
          thCreateDate: '',
          thLastUpdateTime: '',
          thLastUpdator: '',
          thContent: ''
        }, row)
        this.thingForm.thCreateDate = moment(this.thingForm.thCreateDate).toDate()
        this.thingForm.index = index
        this.opts = 'update'
        this.isvisible = true
      // this.$router.replace('/main/thing-edit')
      },
      handleDelete (index, row) {
        let vm = this
        axios.post('/projectmanage/thing/deleteThing', [row.id]).then(function (res) {
          if (res.data.success === true) {
            vm.proData.splice(index, 1)
            vm.$notify({
              title: '事件',
              message: res.data.msg,
              type: 'success',
              duration: 2000
            })
          }
        })
      },
      saveThing (data) {
        let newdata = Object.assign({}, data)
        newdata.proId = this.$store.state.proId
        let vm = this
        if (this.opts === 'create') {
          axios.post('/projectmanage/thing/newThing', newdata).then(function (res) {
            if (res.data.success === true) {
              vm.proData.push(res.data.data)
              vm.isvisible = false
              vm.$notify({
                title: '事件',
                message: res.data.msg,
                type: 'success',
                duration: 2000
              })
            }
          })
        } else {
          axios.post('/projectmanage/thing/updateThing', newdata).then(function (res) {
            if (res.data.success === true) {
              vm.proData.splice(newdata.index, 1, res.data.data)
              vm.isvisible = false
              vm.$notify({
                title: '事件',
                message: res.data.msg,
                type: 'success',
                duration: 2000
              })
            }
          })
        }
      },
      getThing (proId) {
        let vm = this
        axios.get('/projectmanage/thing/thingList', {
          params: {
            proId: proId
          }
        }).then(function (res) {
          if (res.data.success === true) {
            vm.proData = res.data.data
          }
        })
      },
      queryThing () {
        let vm = this
        axios.get('/projectmanage/thing/queryThing', {
          params: this.queryform
        }).then(function (res) {
          if (res.data.success === true) {
            vm.proData = res.data.data
          }
        })
      }
    },
    mounted () {
      this.getThing(this.$store.state.proId)
      // 在这里你想初始化的时候展开哪一行都可以了
      // this.thingForm = Object.assign({}, this.proData[0])
      // this.expands.push(this.proData[0].id)
    }
  }
</script>